﻿CREATE PROCEDURE [dbo].[yaf_post_list](
                @TopicID         INT,
                @UpdateViewCount SMALLINT  = 1)
AS
    BEGIN
        SET nocount  ON
        IF @UpdateViewCount > 0
        UPDATE yaf_Topic
        SET    Views = Views + 1
        WHERE  TopicID = @TopicID
        SELECT   d.TopicID,
                 TopicFlags = d.Flags,
                 ForumFlags = g.Flags,
                 a.MessageID,
                 a.Posted,
                 Subject = d.Topic,
                 a.Message,
                 a.UserID,
                 a.Position,
                 a.Indent,
                 a.IP,
                 a.Flags,
                 UserName = Isnull(a.UserName,b.Name),
                 b.Joined,
                 b.Avatar,
                 b.Location,
                 b.Signature,
                 b.HomePage,
                 b.Weblog,
                 b.MSN,
                 b.YIM,
                 b.AIM,
                 b.ICQ,
                 Posts = b.NumPosts,
                 b.Points,
                 d.Views,
                 d.ForumID,
                 RankName = c.Name,
                 c.RankImage,
                 Edited = Isnull(a.Edited,a.Posted),
                 HasAttachments = (SELECT COUNT(1)
                                   FROM   yaf_Attachment x
                                   WHERE  x.MessageID = a.MessageID),
                 HasAvatarImage = (SELECT COUNT(1)
                                   FROM   yaf_User x
                                   WHERE  x.UserID = b.UserID
                                   AND AvatarImage IS NOT NULL)
        FROM     yaf_Message a
                 JOIN yaf_User b
                   ON b.UserID = a.UserID
                 JOIN yaf_Topic d
                   ON d.TopicID = a.TopicID
                 JOIN yaf_Forum g
                   ON g.ForumID = d.ForumID
                 JOIN yaf_Category h
                   ON h.CategoryID = g.CategoryID
                 JOIN yaf_Rank c
                   ON c.RankID = b.RankID
        WHERE    a.TopicID = @TopicID
        AND (a.Flags & 24) = 16
        ORDER BY a.Posted ASC
    END


